<template>
  <div class="log-container">
    <el-card shadow="never">
      <template #header>
        <div class="table-header">
          <div class="left">
            <h3>操作日志</h3>
          </div>
          <div class="right">
            <el-button type="primary" @click="refresh">
              <el-icon><Refresh /></el-icon>
              刷新
            </el-button>
          </div>
        </div>
      </template>
      
      <!-- 搜索表单 -->
      <el-form :model="searchForm" inline>
        <el-form-item label="操作人">
          <el-input 
            v-model="searchForm.username" 
            placeholder="输入操作人用户名"
            clearable
          />
        </el-form-item>
        
        <el-form-item label="操作类型">
          <el-select 
            v-model="searchForm.action" 
            placeholder="选择操作类型"
            clearable
          >
            <el-option label="登录" value="login" />
            <el-option label="创建" value="create" />
            <el-option label="更新" value="update" />
            <el-option label="删除" value="delete" />
          </el-select>
        </el-form-item>
        
        <el-form-item label="操作时间">
          <el-date-picker
            v-model="searchForm.dateRange"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="YYYY-MM-DD"
          />
        </el-form-item>
        
        <el-form-item>
          <el-button type="primary" @click="handleSearch">搜索</el-button>
        </el-form-item>
      </el-form>
      
      <!-- 日志表格 -->
      <el-table
        :data="logs"
        style="width: 100%"
        border
        v-loading="loading"
      >
        <el-table-column prop="id" label="ID" width="80" />
        <el-table-column prop="username" label="操作人" width="120" />
        <el-table-column prop="action" label="操作类型" width="120">
          <template #default="{row}">
            <el-tag :type="actionType[row.action]">
              {{ actionText[row.action] }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="description" label="操作描述" min-width="200" />
        <el-table-column prop="ip" label="IP地址" width="150" />
        <el-table-column prop="createdAt" label="操作时间" width="180" />
      </el-table>
      
      <!-- 分页 -->
      <div class="pagination">
        <el-pagination
          v-model:current-page="pagination.current"
          v-model:page-size="pagination.size"
          :total="pagination.total"
          @current-change="handlePageChange"
          layout="total, prev, pager, next"
        />
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { getOperationLogs } from '@/api/admin'
import { Refresh } from '@element-plus/icons-vue'

// 操作类型文本映射
const actionText = {
  login: '登录',
  create: '创建',
  update: '更新',
  delete: '删除',
  other: '其他'
}

// 操作类型标签样式
const actionType = {
  login: 'success',
  create: '',
  update: 'warning',
  delete: 'danger',
  other: 'info'
}

// 搜索表单
const searchForm = ref({
  username: '',
  action: '',
  dateRange: []
})

// 分页
const pagination = ref({
  current: 1,
  size: 20,
  total: 0
})

// 数据
const logs = ref([])
const loading = ref(false)

// 加载日志
const loadLogs = async () => {
  try {
    loading.value = true
    const params = {
      ...searchForm.value,
      page: pagination.value.current,
      size: pagination.value.size
    }
    
    // 处理日期范围
    if (searchForm.value.dateRange && searchForm.value.dateRange.length === 2) {
      params.startDate = searchForm.value.dateRange[0]
      params.endDate = searchForm.value.dateRange[1]
    }
    
    const res = await getOperationLogs(params)
    logs.value = res.data.items
    pagination.value.total = res.data.total
  } catch (error) {
    console.error('加载操作日志失败:', error)
    ElMessage.error('加载操作日志失败')
  } finally {
    loading.value = false
  }
}

// 搜索
const handleSearch = () => {
  pagination.value.current = 1
  loadLogs()
}

// 刷新
const refresh = () => {
  loadLogs()
}

// 分页变化
const handlePageChange = () => {
  loadLogs()
}

onMounted(() => {
  loadLogs()
})
</script>

<style scoped>
.log-container {
  padding: 20px;
}

.table-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.pagination {
  margin-top: 20px;
  text-align: right;
}
</style>